/**
 * Created by qqlai on 2016/11/22.
 */
{
    angular.module('stayOutLateModule')
        .controller('StayOutLateController',StayOutLateController);
    StayOutLateController.$inject = ['http.interfaces','$state'];
    function StayOutLateController(interfaces,$state) {
        let vm = this;
        let collect = {
            list:[],
            selected:{},
            selectedDate:'',
            inf:[],
            time:[],
            dateList:[],
            toDay:'',
            countAll:'',
            idList:[],
            toggle:false,
            active: Active,
            Changed:Changed,
            dateChange:dateChange,
            outLateYesterday:outLateYesterday,
            stayOutLate:stayOutLate,
            logout:logout
        };
        collect.active();//进入控制器后调用一次
        vm.collect = collect;//输出集合，提供给模板调用


        /** Active方法
         * @desc 控制器触发时执行一次的方法
         * @constructor
         */
        function Active() {
            collect.outLateYesterday();
        }

        function outLateYesterday(date) {
            var param={};
            if(date!=undefined){
                param={search_Date:date};
            }
            interfaces.outLateYesterday(param).then(res=>{
                if(res.data.resultCode=="1"){
                    collect.list=res.data.data.list;
                    if(date==undefined){
                        for(var x=0;x<7;x++){
                            var time= new Date(res.data.data.time).getTime()-x*24*60*60*1000;
                            var year=new Date(time).getFullYear();
                            var month=new Date(time).getMonth()+1;
                            var day=new Date(time).getDate();
                            if(month<10) month="0"+month;
                            if(day<10)　 day="0"+day;
                            collect.dateList[x]=year+'-'+month+'-'+day;
                           
                        }
                        collect.toDay=res.data.data.time;
                        collect.selectedDate=res.data.data.time;
                    }
                    collect.countAll=res.data.data.countAll;
                    collect.list.unshift({
                        "departName": "全部",
                        "departId": 1
                    },);
                    collect.selected = collect.list[0];
                    // 指定图表的配置项和数据
                    var option = {
                        barWidth: 18,
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                            }
                        },
                        legend: {
                            data: ['晚归']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'value'
                        },
                        yAxis: {
                            type: 'category',
                            data: []
                        },
                        series: [
                            {
                                name: '晚归人数',
                                type: 'bar',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: true,
                                        position: 'insideRight'
                                    }
                                },
                                data: []

                            }
                        ],
                        color: ['rgb(191,85,93)']

                    };
                    collect.idList=[];
                    collect.time=[];
                    for (var x = 0; x < res.data.data.list.length; x++) {
                        if(res.data.data.list[x].departCount>=0){
                            if (res.data.data.list[x].departName.length > 6) {
                                option.yAxis.data.push(res.data.data.list[x].departName.replace(/(.{6})/g, '$1\n'));

                            } else {
                                option.yAxis.data.push(res.data.data.list[x].departName);
                             
                            }
                            collect.idList.push(res.data.data.list[x].departId);
                            option.series[0].data.push(res.data.data.list[x].departCount);
                        }
                    }
                    angular.element(document.querySelector("#eWg")).height( option.series[0].data.length*55+55);
                   
                    var myChart = echarts.init(document.getElementById('eWg'));
                    myChart.on('click', function (params) {
                        interfaces.time=collect.selectedDate;
                        $state.go('tab.studentStayOutLateList',{id:collect.idList[params.dataIndex]});
                    });
                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption(option);
                }
            })
        }

        function stayOutLate(id) {
            interfaces.stayOutLate({departId:id}).then(res=>{
                if(res.data.resultCode=="1"){
                    collect.countAll=res.data.data[6].count;
                    // 指定图表的配置项和数据
                    var option = {
                        tooltip : {
                            trigger: 'axis'
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis : [
                            {
                                type : 'category',
                                boundaryGap : false,
                                data : []
                            }
                        ],
                        yAxis : [
                            {
                                type : 'value'
                            }
                        ],
                        series : [
                            {
                                name:'晚归人数',
                                type:'line',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: true,
                                        position: 'top'
                                    }
                                },
                                areaStyle: {normal: {}},
                                data:[]
                            }
                        ],

                        color: ['rgb(191,85,93)']

                    };
                    for (var x = 0; x < res.data.data.length; x++) {
                            option.xAxis[0].data[x] = res.data.data[x].time.substring(6);
                            option.series[0].data[x] = res.data.data[x].count;
                    }
                    var myChart = echarts.init(document.getElementById('rWg'));
                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption(option);
                }
            })
        }

        function Changed() {
            if (collect.selected.departId == 1) {
                collect.toggle=false;
                collect.outLateYesterday(collect.selectedDate);
            } else {
                collect.toggle=true;
                collect.stayOutLate(collect.selected.departId);
            }
        }
        
        function dateChange() {
            if (collect.selectedDate == collect.toDay) {
                collect.outLateYesterday();
            } else {
                collect.outLateYesterday(collect.selectedDate);
            }
        }

        function logout() {
            interfaces.logout();
        }
    }
}